package System.Task;

import patterns.command.TaskPriority;
import patterns.command.TaskStatus;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class Task {
    private String taskId;
    private String description;
    private String machineId;
    private String fieldId;
    private TaskStatus status;
    private TaskPriority priority;
    private LocalDateTime createTime;
    private LocalDateTime startTime;
    private LocalDateTime endTime;
    private List<String> commands = new ArrayList<>();

    public Task(String taskId, String description, String machineId, String fieldId, TaskPriority priority) {
        this.taskId = taskId;
        this.description = description;
        this.machineId = machineId;
        this.fieldId = fieldId;
        this.priority = priority;
        this.status = TaskStatus.PENDING;
        this.createTime = LocalDateTime.now();
    }

    // Getters
    public String getTaskId() { return taskId; }
    public String getDescription() { return description; }
    public String getMachineId() { return machineId; }
    public String getFieldId() { return fieldId; }
    public TaskStatus getStatus() { return status; }
    public TaskPriority getPriority() { return priority; }
    public LocalDateTime getCreateTime() { return createTime; }
    public LocalDateTime getStartTime() { return startTime; }
    public LocalDateTime getEndTime() { return endTime; }
    public List<String> getCommands() { return commands; }

    // 状态变更方法
    public void start() {
        if (status != TaskStatus.PENDING && status != TaskStatus.PAUSED) {
            throw new IllegalStateException("任务当前状态不能开始");
        }
        status = TaskStatus.EXECUTING;
        startTime = LocalDateTime.now();
    }

    public void pause() {
        if (status != TaskStatus.EXECUTING) {
            throw new IllegalStateException("只有执行中的任务可以暂停");
        }
        status = TaskStatus.PAUSED;
    }

    public void resume() {
        if (status != TaskStatus.PAUSED) {
            throw new IllegalStateException("只有暂停的任务可以恢复");
        }
        status = TaskStatus.EXECUTING;
    }

    public void complete() {
        status = TaskStatus.COMPLETED;
        endTime = LocalDateTime.now();
    }

    public void fail(String reason) {
        status = TaskStatus.FAILED;
        endTime = LocalDateTime.now();
        commands.add("失败原因: " + reason);
    }

    public void cancel() {
        status = TaskStatus.CANCELLED;
        endTime = LocalDateTime.now();
    }

    public void addCommand(String command) {
        commands.add(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_TIME) + " - " + command);
    }

    public void display() {
        System.out.println("=== 任务详情 ===");
        System.out.println("任务ID: " + taskId);
        System.out.println("描述: " + description);
        System.out.println("农机ID: " + machineId);
        System.out.println("农田ID: " + fieldId);
        System.out.println("优先级: " + priority.getDescription());
        System.out.println("状态: " + status.getDescription());
        System.out.println("创建时间: " + createTime.format(DateTimeFormatter.ISO_LOCAL_TIME));
        if (startTime != null) {
            System.out.println("开始时间: " + startTime.format(DateTimeFormatter.ISO_LOCAL_TIME));
        }
        if (endTime != null) {
            System.out.println("结束时间: " + endTime.format(DateTimeFormatter.ISO_LOCAL_TIME));
        }

        if (!commands.isEmpty()) {
            System.out.println("\n任务指令记录:");
            for (String cmd : commands) {
                System.out.println(" - " + cmd);
            }
        }
    }
}
